//
//  AppDelegate.m
//  j绝版新闻客户端
//
//  Created by weige on 16/5/7.
//  Copyright © 2016年 weige. All rights reserved.
//

#import "AppDelegate.h"
#import "GuideViewController.h"
#import "ADViewController.h"
#import "MainTabBarController.h"
#import "LoveHomeViewController.h"
#import "LoveViewController.h"
@interface AppDelegate ()
{
    LoveHomeViewController  *loev;
}
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
   // NSLog(@"%@",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]);
    //NSLog(@"%@",[[NSBundle mainBundle]bundlePath]);
    [self bulidKeyWindow];
    [self addNotifiCation];
    [self CreataDB];
    UIApplication *app=[UIApplication sharedApplication];
    app.applicationIconBadgeNumber=123;
    return YES;
}


-(void)CreataDB
{
    self.db=[FMDatabase databaseWithPath:[ContentFileFillPath ContentFileFillPathsCroe:@"new.data"]];
   // NSLog(@"%@",[ContentFileFillPath ContentFileFillPathsCroe:@"new.data"]);
    if ([self.db open]==YES) {
        
        //创建表
        if([self.db executeUpdate:@"create table if not exists t_news(idid integer not null primary key,title text not null,subtitle text not null,time text not null,content text not null,flid interger not null,author text not null,clicks integer not null,data blob null)"]==YES)
        {
            
           // NSLog(@"成功");
        }
    }
}

-(void)bulidKeyWindow
{
    self.window=[[UIWindow alloc]init];
    NSString *isFristOpen=[[NSUserDefaults standardUserDefaults]objectForKey:@"isFristOpen"];
    //NSLog(@"%@",isFristOpen);
    if (isFristOpen==nil) {
        GuideViewController *gcvc=[[GuideViewController alloc]init];
        self.window.rootViewController=gcvc;
        [self.window makeKeyAndVisible];
        [[NSUserDefaults standardUserDefaults]setObject:@"isFristOpen" forKey:@"isFristOpen"];
    }
    else
    {
        [self loadADRootViewController];
    }
    
}

//加载ad
-(void)loadADRootViewController
{
    ADViewController *advc=[[ADViewController alloc]init];
    self.window.rootViewController=advc;
    [self.window makeKeyAndVisible];
}
//通知中心

-(void)addNotifiCation
{
    //这个是ad加载成功干什么
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showMainTabbarControllerSucess:) name:ADImageLoadSecussed object:nil];
    //ad加载失败
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showMainTabbarControllerFale) name:ADImageLoadFail object:nil];
    //成功加载完引导干什么
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(shoMainTabBarController) name:GuideViewControllerDidFinish object:nil];
    
}


-(void)showMainTabbarControllerSucess:(NSNotification *)note
{
    UIImage *img=(UIImage *)note.object;
    // NSLog(@"%@",img);
   // LoveHomeViewController *love=[[LoveHomeViewController alloc]init];
    //MainTabBarController *tab=[[MainTabBarController alloc]init];
    // tab.tabimag=img;
    //tab.name=@"sdadsadasdasdasda";
    LoveViewController *love=[[LoveViewController alloc]init];
    love.tabimag=img;
    self.window.rootViewController=love;
    [self.window makeKeyAndVisible];
    
}
-(void)showMainTabbarControllerFale
{
   // MainTabBarController *tab=[[MainTabBarController alloc]init];
    // tab.tabimag=img;
    //tab.name=@"sdadsadasdasdasda";
    LoveViewController *love=[[LoveViewController alloc]init];
    self.window.rootViewController=love;
    //self.window.rootViewController=tab;
    [self.window makeKeyAndVisible];
}
-(void)shoMainTabBarController
{
  //  MainTabBarController *tab=[[MainTabBarController alloc]init];
    // tab.tabimag=img;
    //tab.name=@"sdadsadasdasdasda";
    LoveViewController *love=[[LoveViewController alloc]init];
    
    self.window.rootViewController=love;
    //self.window.rootViewController=tab;
    [self.window makeKeyAndVisible];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
